<?php

class IndexController extends Zend_Controller_Action {

    public function init() {
        
    }

    public function indexAction() {
        $input = new Application_Model_Form_Message(); //array('disableLoadDefaultDecorators' => true));
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
        $ip = $_SERVER['REMOTE_ADDR'];
        $this->view->input = $input;
        $this->view->txt_uri_icon = '/upload/default/file.jpg';
        $row = new Application_Model_Msg();
        if ($this->getRequest()->isPost()):
            $formData = $this->getRequest()->getPost();
            if ($input->isValid($formData)):
                //var_dump($input->img->getFileName () );  
                if ($input->img_name->getFileName()):
                    $img_uri = str_replace('\\', '/', str_replace(PUBLIC_PATH, "", $input->img_name->getFileName()));
                else:
                    $img_uri = '/upload/default/img.jpg';
                endif;
                if ($input->txt_name->getFileName()):
                    $txt_uri = str_replace('\\', '/', str_replace(PUBLIC_PATH, "", $input->txt_name->getFileName()));
                endif;
                $values = $input->getValues();
                $row->fill($values);
                $row->ip = $ip;
                $row->browser = $this->getBrowser($userAgent);
                $row->dt = time();
                $row->img_uri = $img_uri;
                $row->txt_uri = $txt_uri;
                $row->save();
                $this->_redirect('/');
            endif;
        endif;

        $msg = new Application_Model_Msg();
        $sort_by = $this->_getParam('sort_by');
        $dir = $this->_getParam('dir');
        $this->view->sort_by = $sort_by;
        $this->view->dir = $dir;
        if ($dir == 'desc'):
            $this->view->msg = $msg->getContent('desc', $sort_by);
            $this->view->arrow = '↓';
        elseif ($dir == 'asc'):
            $this->view->msg = $msg->getContent('asc', $sort_by);
            $this->view->arrow = '↑';
        else:
            $this->view->msg = $msg->getContent('DESC', 'id');
            $this->view->url = FALSE;
        endif;
    }

    public function getBrowser($userAgent) {
        // Список браузеров
        $browsers = array(
            'Opera' => 'Opera',
            'Mozilla Firefox' => '(Firebird)|(Firefox)',
            'Google Chrome' => 'Chrome',
            'Galeon' => 'Galeon',
            'Mozilla' => 'Gecko',
            'MyIE' => 'MyIE',
            'Lynx' => 'Lynx',
            'Konqueror' => 'Konqueror',
            'SearchBot' => '(Googlebot)|(Slurp/cat)|(msnbot)|(ia_archiver)',
            'Internet Explorer 9' => '(MSIE 9.[0-9]+)',
            'Internet Explorer 8' => '(MSIE 8.[0-9]+)',
            'Internet Explorer 7' => '(MSIE 7.[0-9]+)',
            'Internet Explorer 6' => '(MSIE 6.[0-9]+)',
            'Internet Explorer 5' => '(MSIE 5.[0-9]+)',
            'Internet Explorer 4' => '(MSIE 4.[0-9]+)',
        );

        foreach ($browsers as $browser => $pattern) :
            if (preg_match('/' . $pattern . '/', $userAgent)):
                return $browser;
            endif;
        endforeach;
        return 'Unknown'; // Версию браузера установить не удалось
    }

    public function validateformAction() {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();

        $f = new Application_Model_Form_Message();
        $f->isValid($this->_getAllParams());
        $json = $f->getMessages();
        header('Content-type: application/json');
        echo Zend_Json::encode($json);
    }

}

